package com.qf.index;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ZipUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

public class TestW {

    public static void main(String[] args) throws SQLException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String newDataName = "scholar_25_02.";
        String summaryTable = newDataName + "unified_search_summary_0309";
        String summaryNewTable = newDataName + "unified_search_summary_0309_2";


        String save = "`cscd_id`,`ncbi_id`,`yiigle_id`,`wos_id`,`weipu_id`,`wanfang_id`,`cnki_id`,`key_word_list`,`journal`,`aid`,`citation_quota`,`reference_quota`,`ti_key`,`ab_key`,`kw_key`,`abb_word`,`zky_dalei`,`jcr_if`,`year`,`title`,`keyword`,`ab`,`mesh`,`jid`," +
                "`discussion`,`results`,`methods`,`background`,`c1_au`,`rp_au`,`cn_ti`,`cn_ab`,`cn_kw`,`pt`,`is_cns`,`pmId`,`ut`,`doi`,`nlmId`,`cite_score`,`jcr_quartile`,`fund_list`,`drug_gene_pathway_disease`,`auth`,`info`,`is_retraction`,`is_china`," +
                "`cn_discussion`,`cn_results`,`cn_methods`,`cn_background`,`cs_sjr`,`is_beida`,`is_cs`,`is_cscd`,`is_jcr`,`is_pubmed`,`is_tjy`,`is_yiigle`,`is_zky`,`zky_subject`,`is_core`,`mail_list`,`subject`,`kw_plus`,`times_cited`,`language`,`references`,`citations`," +
                "`is_china_hosp`,`is_guidelines`,`is_pubmed_article`,`is_6year`,`pm_ad`,`ncbi_subject`,`ncbi_journal`,`ncbi_mesh`,`web_site`,`cn_country`,`early_warning`";

        String sql = "insert into " + summaryNewTable + " (" + save + ") SELECT " + save + " from " + summaryTable + " where id = " + 1 + " ;";
        System.out.println(sql);
//        Db.use().execute("insert into " + summaryNewTable + " (" + save + ") SELECT " + save + " from " + summaryTable + " where id = " + 1 + " ;");

//        readTableRange(summaryTable, 10000, "", "id", "aid", true, 10, summaryNewTable);


    }

//    public static void readTableRange2(String tableName, int onePageNum, String limit, String autoId, String field, boolean print, int allTotal) throws SQLException {
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        List<Entity> tableNum = Db.use().query("select count(1) as count from " + tableName);
//        if (tableNum.get(0).getInt("count") > 0) {
//            int tableMax = Db.use().queryNumber("select max(" + autoId + ") from " + tableName).intValue();
//            Collection<Integer> tableList = new ArrayList<>();
//            NumberUtil.appendRange(0, tableMax + onePageNum, onePageNum, tableList);
//            AtomicInteger sumNum = new AtomicInteger(0);
//            int showNum = Math.max((tableList.size() / allTotal), 1) + (tableList.size() % allTotal);
//            AtomicInteger startNum = new AtomicInteger(1);
//
//            // 使用虚拟线程执行器替换parallelStream
//            try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
//                for (Integer startId : tableList) {
//                    executor.submit(() -> {
//                        try {
//                            List<Entity> tableData = Db.use().query("select `" + autoId + "`,`" + field + "` from " + tableName
//                                    + " where " + autoId + " between " + startId + " and " + (startId + onePageNum - 1) + limit);
//
//                            if (print && sumNum.getAndIncrement() % showNum == 0) {
//                                System.out.println("\033[31;1m" + format.format(new Date())
//                                        + " 读表现在开始：" + startNum.getAndIncrement()
//                                        + " 总次数：" + allTotal
//                                        + " 表名：" + tableName
//                                        + Utils.printMemoryStr() + "\033[0m");
//                            }
//
//                            for (Entity entry : tableData) {
//                                // 逻辑处理单元
//                            }
//                        } catch (SQLException e) {
//                            e.printStackTrace();
//                        }
//                    });
//                }
//            } // 这里会自动等待所有任务完成
//        }
//    }

    public static void readTableRange(String summaryTable, int onePageNum, String limit, String autoId, String field, boolean print, int allTotal, String summaryNewTable) throws SQLException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<Entity> tableNum = Db.use().query("select count(1) as count from " + summaryTable);

        String save = "`cscd_id`,`ncbi_id`,`yiigle_id`,`wos_id`,`weipu_id`,`wanfang_id`,`cnki_id`,`key_word_list`,`journal`,`aid`,`citation_quota`,`reference_quota`,`ti_key`,`ab_key`,`kw_key`,`abb_word`,`zky_dalei`,`jcr_if`,`year`,`title`,`keyword`,`ab`,`mesh`,`jid`," +
                "`discussion`,`results`,`methods`,`background`,`c1_au`,`rp_au`,`cn_ti`,`cn_ab`,`cn_kw`,`pt`,`is_cns`,`pmId`,`ut`,`doi`,`nlmId`,`cite_score`,`jcr_quartile`,`fund_list`,`drug_gene_pathway_disease`,`auth`,`info`,`is_retraction`,`is_china`," +
                "`cn_discussion`,`cn_results`,`cn_methods`,`cn_background`,`cs_sjr`,`is_beida`,`is_cs`,`is_cscd`,`is_jcr`,`is_pubmed`,`is_tjy`,`is_yiigle`,`is_zky`,`zky_subject`,`is_core`,`mail_list`,`subject`,`kw_plus`,`times_cited`,`language`,`references`,`citations`," +
                "`is_china_hosp`,`is_guidelines`,`is_pubmed_article`,`is_6year`,`pm_ad`,`ncbi_subject`,`ncbi_journal`,`ncbi_mesh`,`web_site`,`cn_country`,`early_warning`";

        if (tableNum.get(0).getInt("count") > 0) {
            int tableMax = Db.use().queryNumber("select max(" + autoId + ") from " + summaryTable).intValue();
            Collection<Integer> tableList = new ArrayList<>();
            NumberUtil.appendRange(0, tableMax + onePageNum, onePageNum, tableList);
            AtomicInteger sumNum = new AtomicInteger(0);
            int showNum = Math.max((tableList.size() / allTotal), 1) + (tableList.size() % allTotal);
            AtomicInteger startNum = new AtomicInteger(1);
            tableList.parallelStream().forEach(startId -> {
                try {
                    List<Entity> tableData = Db.use().query("select `" + autoId + "`,`" + field + "` from " + summaryTable + " where " + autoId + " between " + startId + " and " + (startId + onePageNum - 1) + limit);
                    if (print && sumNum.getAndIncrement() % showNum == 0) {
                        System.out.println("\033[31;1m" + format.format(new Date()) + " 读表现在开始：" + startNum.getAndIncrement() + " 总次数：" + allTotal + " 表名：" + summaryTable + Utils.printMemoryStr() + "\033[0m");
                    }
                    for (Entity entry : tableData) {
                        Integer id = entry.getInt(autoId);
                        // insert into pm_ut_aid_info_1w (`c1_list`, `rp_list`, `pmId`) SELECT `c1_list`, `rp_list`, 100 from pm_ut_aid_info where id =1;
//                        String sql = "insert into " + summaryNewTable + " (" + save + ") SELECT " + save + " from " + summaryTable + " where id = " + id + ";";
                        Db.use().execute("insert into " + summaryNewTable + " (" + save + ") SELECT " + save + " from " + summaryTable + " where id = " + id + ";");

                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            });
        }
    }


}
